package org.example.util;

import co.elastic.clients.elasticsearch._types.query_dsl.*;
import org.apache.commons.lang3.StringUtils;
import org.example.vo.FileQuery;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class EsQueryUtil {
    private final FileQuery fileQuery;
    private final List<Query> queries;

    public EsQueryUtil(FileQuery fileQuery) {
        this.fileQuery = fileQuery;
        queries = new ArrayList<>();
    }

    private void userId() {
        if (Objects.nonNull(fileQuery.getUserId()) && !fileQuery.getUserId().equals(0L)) {
            queries.add(TermQuery.of(t -> t
                    .field("userId")
                    .value(fileQuery.getUserId())
            )._toQuery());
        }
    }

    private void content() {
        if (StringUtils.isNotBlank(fileQuery.getKeyword())) {
            queries.add(MatchPhraseQuery.of(x -> x
                            .field("content")
                            .query(fileQuery.getKeyword()))
                    ._toQuery());
        }
    }

    public List<Query> getQueries() {
        userId();
        content();
        return queries;
    }
}
